libname ra "E:\data";/*cohort*/
libname pd "Z:\Medicare_20\Part_D";
libname pb "Z:\Medicare_20\Part_b";

*****************PURPOSE:IDENTIFY DESCALATION EVENT FOR EACH  INDEX PART D MEDICATION 
                  1. Flag and identify dose interval increase (days supply/quan dispensed)
                     confirm stable use 6m prior to possible interval increase, confirm sustained interval increase in next fill dt
                     and get corresponding date of interval increase.
                   2. Flag and identify dose reduction (only rx with dose change is ETANERCEPT)
                     confirm stable use 6m prior to possible dose reduction, confirm sustained dose reduction
                     in next fill dt and get corresponding date of dose reduction 
                    2. Flag and identify cessation and get dates
                    3. Identify the first descalation per each id*****************;

**********************ETANERCEPT IS THE ONLY MEDICATION WITH DOSE CHANGE*********************;
data etan;
set ra.coh_ana5a;
 where index_med1= "Etanercept" ;
   drop yr_partd yr_b  SRVC_DT  PDE_ID Medication_name diff_d 
   med_name diff_b diff_d  RA_b_icd;

/*FLAG OBSRVATIONS with ANY increase interval (6 months after index for partD) */
   if index_lagrx_int ge 183 and diff_dose_int gt 0 then inc_dose_int=1;

 /*if increase dose int occurs during swtiching it qualifies as cessation,not interval increase*/

   if index_lagrx_int ge 183 and diff_dose_int gt 0 and (name_med ne lag_name_med) then inc_dose_int=.;

   if inc_dose_int=1 then incint_dt= lag_rx_dt;/*use the date of last time when interval is stable*/
	
   /* get event date minus 6 months to asses stability*/
    incint_dt_6m = intnx('DAY',incint_dt,-183);

  /*no event in first 6 months after index30*/

    if index_lagrx_int lt 183 then do;

     incint_dt = . ; 
     incint_dt_6m = .;
	 inc_dose_int= .;
    
     end;
  
   /*dose reduction, 6 months after index dt, if dose less than prev but not mssing*/
  if index_lagrx_int ge 183 and dose_ch lt 0 and dose_ch ne . then dose_red=1;

   if dose_red=1 then dose_red_dt= lag_rx_dt;/*use the date of last time when dose was stable*/

/* event date minus 6 months to asses stability*/
   dose_red_dt_6m = intnx('DAY',dose_red_dt,-183);

  if index_lagrx_int lt 183 then do;

     dose_red_dt = . ; 
     dose_red_dt_6m = .;
     dose_red= .; 
 
     end;

format incint_dt date9. incint_dt_6m date9.  dose_red_dt date9. dose_red_dt_6m date9. ;


 run;
 
**********INCREASED INTERVAL, DECREASE DOSE AND  STABILITY OF USE 6m BEFROE************************;

/* SORT by descending***/

proc sort data= etan out= etan_1 ;
  by bene_id descending rx_dt descending incint_dt ; run;

/*create time interval for 6 m prior to event date
 /*make incint_dt and incint_dt_6m same per each int increase**/

data etan_1;
set etan_1;
  by bene_id;
  retain incint_dt_6m2 incint_dt2 ;

 if first.bene_id then do;/*setting to . at the begining for each id*/

   incint_dt_6m2= .;
 
   incint_dt2=. ;
 
 end;

 if incint_dt_6m > . then incint_dt_6m2= incint_dt_6m ;/*if missing then carry forward(backward)*/
 else incint_dt_6m = incint_dt_6m2;/*else keep the prev value*/

 if incint_dt > . then incint_dt2= incint_dt ;/*if missing then carry forward(backward)*/
 else incint_dt = incint_dt2;/*else keep the prev value*/

drop   incint_dt2 incint_dt_6m2 ;

run;

/**sort back by  rx_dt to regular chron order*/
proc sort data= etan_1 out= etan_2;by bene_id rx_dt ;run;

data etan_2;
   set etan_2;
   by bene_id;

 rx_incint6 = rx_dt - incint_dt_6m;/* int between rx date and event dt-6m date*/

/* new var to code stable int,  set to missing at the time of event, and obs beyond 6 month dt
so stable int can be assessed from obs before event until 6 mothns before event*/

if rx_incint6 ge 0 and rx_incint6 le 183 then int_6=1; else int_6=0;

/*create another var diff_dose_int1 for diff, so that this var is non missing during the interval of interest*/
 
if rx_incint6 lt 0  or rx_incint6 gt 183 then diff_dose_int1 = . ;/*183 is the time of event*/
else diff_dose_int1= diff_dose_int;/*outside 6m window, set to . else take value of diff_int_w */


/*lag vars*/
lag_rx_incint6= lag (rx_incint6);
 lag_int6= lag (int_6);
 lag_rx_int= lag(rx_int);

if first.bene_id then do ;

  lag_rx_incint6= .;
  lag_int6= .;
 lag_rx_int= . ;

 end;

 ****code first diff in the interval between inc dt and inc dt -6m as missing, as
 this value lies outside of the time interval of interest*;

   if int_6 ne lag_int6 then diff_dose_int1= . ; 
 

 /*use absolute value to calculate sum of diff so that -1 and 1 diff doesnt sum upto zero*/

   diff_dose_int2 = abs (diff_dose_int1); 

  
run;

*******************interval increase******************;
******************stable use prior*********************;

proc sort data= etan_2 out= etan_int  ;by bene_id  incint_dt; run;

/*get sum of diff int for each potential event date
  value >0 indicates unstable use */
proc means data= etan_int sum noprint;
    by bene_id incint_dt;
    var diff_dose_int2 ;
    output out=sum_diff 
        sum=Sum_diff_int ;
run;


/*get sum of numbers of days between each potential event date 
 ( for first event it is number of days since index date
event flag are coded such that the event cannot happen in 6 months since index30 date)*/
proc means data=etan_int sum noprint;
    by bene_id incint_dt;
    var lag_rx_int;
    output out=sum_rx_int 
        sum=Sum_rx_int ;
run;


proc sort data= sum_rx_int out= sum_1 (drop= _TYPE_ _FREQ_) ; by bene_id incint_dt;run;
proc sort data= sum_diff out= sum_2 (drop= _TYPE_ _FREQ_) ; by bene_id incint_dt;run;
proc sort data= etan_2; by bene_id incint_dt ; run;

data etan_3;
merge etan_2 sum_1 sum_2;
  by bene_id incint_dt;

  lag_days_suply= lag (DAYS_SUPLY_NUM);

  if first.bene_id then lag_days_suply= .;

  SUPPLY=  rx_int - lag_days_suply;
  ***************interval increase with confirmed stable use prior***********;
****************NON SWITCHERS*************;

if idswitch ne 1 then do;

/* increase int AND stable diff in past 6 m*/
if inc_dose_int =1 and sum_diff_int=0 and sum_rx_int ge 183 then event_incint=1;
else event_incint=0;

end;

/*******SWITCHERS, look for event during index med only**********/
if idswitch =1 then do;

if  inc_dose_int=1 and sum_diff_int=0 and sum_rx_int ge 183 and incint_dt < switch_dt 
then event_incint=1;/*if increase int happened before switch*/
else event_incint=0;

end;

***********IF INCREASE INTERVAL QUALIFIES AS CESSATION**************;
/*gap >90, then it qualifies for cessation and not inc interval*/

  if  SUPPLY gt 90 then event_incint = . ;


run;/*event date is incint_dt*/

proc sort data= etan_3 ; by bene_id rx_dt;
run;/*sort by regular order*/

**************sustained inc int for next visit and cessation***************;

****sort by desc to pull the next obs using lag function***;
proc sort data= etan_3 out=sus;  by bene_id descending rx_dt ;run;

data sus;
set sus;
  next1_diff_int = lag( diff_dose_int);
  next2_diff_int = lag (next1_diff_int);

  if event_incint=1;
  /*keeping only obs at time of event; lag and lag of lag by descending to pull next1 and 2 obs*/

keep bene_id rx_dt diff_int_w next1_diff_int next2_diff_int;
run;

/*sort by regular order*/
proc sort data= sus; by bene_id rx_dt;run;
proc sort data= etan_3; by bene_id rx_dt;run;

data etan_3;
merge etan_3 sus;
by bene_id rx_dt;

/*sustained in next 1 rx dt*/
if event_incint=1 and next1_diff_int lt 0 then event_incint_sus1=0;
else if event_incint=1 and next1_diff_int  ge 0 then event_incint_sus1=1;

/*sustained in next 2 rx dt*/
if event_incint=1 and event_incint_sus1=1 and  next2_diff_int lt 0 then event_incint_sus2=0;
else if event_incint=1 and event_incint_sus1=1 and next2_diff_int ge 0 then event_incint_sus2=1;

if event_incint_sus1 =1 then sus1_incint_dt= incint_dt;

format  sus1_incint_dt date9. ;
run;


*******************************************************************************************
********************************Dose reduction****************************************
******************************************************************************************;

********************assess stability of use***********************;
/* SORT by descending***/
proc sort data= etan_3 out= etan_4 ;
  by bene_id descending rx_dt descending dose_red_dt ; run;

/*create time interval for 6 m prior to event date*/
data etan_4;
set etan_4;
 by bene_id;
 retain dose_red_dt_6m2 dose_red_dt2 ;

 if first.bene_id then do;/*setting to . at the begining for each id*/

 dose_red_dt_6m2= .;
 
 dose_red_dt2=. ;
 
 end;

 if dose_red_dt_6m > . then dose_red_dt_6m2= dose_red_dt_6m ;/*if missing then carry forward(backward)*/
 else dose_red_dt_6m = dose_red_dt_6m2;/*else keep the prev value*/


 if dose_red_dt > . then dose_red_dt2= dose_red_dt ;/*if missing then carry forward(backward)*/
 else dose_red_dt = dose_red_dt2;/*else keep the prev value*/


drop   dose_red_dt2 dose_red_dt_6m2 ;

run;

/**sort back by  rx_dt to regular chron order*/
proc sort data= etan_4 out= etan_5;by bene_id rx_dt ;run;

data etan_5;
set etan_5;
by bene_id;


rx_dosered6 = rx_dt - dose_red_dt_6m;/* int between rx date and event dt-6m date*/


/* new var to code stable int,  set to missing at the time of event, and obs beyond 6 month dt
so stable int can be assessed from obs before event until 6 mothns before event*/

if rx_dosered6 ge 0 and rx_dosered6 le 183 then dose_6=1; else dose_6=0;

/*create another var , so that this var is non missing during the interval of interest*/
 
if rx_dosered6 lt 0  or rx_dosered6 gt 183 then dose_ch1 = . ;/*183 is the time of event*/
else dose_ch1= dose_ch; /*outside 6m window, set to . else take value of diff_int_w */


 lag_rx_dosered6= lag (rx_dosered6);
 lag_dose6= lag (dose_6);

  if first.bene_id then  do;
  lag_int6= .;
  lag_rx_incint6= .;
  end;
 

 ****code first diff in the interval between inc dt and inc dt -6m as missing, as
 this value lies outside of the time interval of interest*;

 if dose_6 ne lag_dose6 then dose_ch1= . ; 
 

 /*use absolute value to calculate sum of diff so that -1 and 1 diff doesnt sum upto zero*/

 dose_ch2 = abs (dose_ch1); 

 run;


******************stable use prior*********************;

proc sort data= etan_5 out= etan_dose ;by bene_id  dose_red_dt; run;

/*get sum of diff int for each potential event date
  value >0 indicates unstable use */
proc means data = etan_dose sum noprint;
by bene_id dose_red_dt;
    var dose_ch2 ;
    output out = sum_dose 
        sum=Sum_dose_ch ;
run;



/*get sum of numbers of days between each potential event date 
 ( for first event it is number of days since index date
event flag are coded such that the event cannot happen in 6 months since index30 date)*/
proc means data=etan_dose sum noprint;
    by bene_id dose_red_dt;
    var lag_rx_int;
    output out=sum_rx_int1 
        sum=Sum_rx_int1 ;
run;


proc sort data= sum_rx_int1 out= sum_3 (drop= _TYPE_ _FREQ_) ; by bene_id dose_red_dt;run;
proc sort data= sum_dose out= sum_4 (drop= _TYPE_ _FREQ_) ; by bene_id dose_red_dt;run;
proc sort data= etan_5; by bene_id dose_red_dt ; run;

data etan_6;
merge etan_5 sum_3 sum_4;
by bene_id dose_red_dt;

****************NON SWITCHERS*************;

if idswitch ne 1 then do;

if dose_red =1 and sum_dose_ch=0 and sum_rx_int1 ge 183 
then event_dosered=1;/* dose reduction AND stable diff in past 6 m*/
else event_dosered=0;

end;

/*******SWITCHERS, look for event during index med only**********/
if idswitch =1 then do;

if  dose_red=1 and sum_dose_ch=0 and sum_rx_int1 ge 183 and dose_red_dt < switch_dt 
then event_dosered=1;/*if increase int happened before switch*/

else event_dosered=0;/*not stable/ not during index med*/

end;

***********IF INCREASE INTERVAL QUALIFIES AS CESSATION**************;
/*gap >90, then it qualifies for cessation and not inc interval*/

  if  SUPPLY gt 90 then event_incint = . ;


run;/*event date is dose_red_dt*/

proc sort data= etan_6 ; by bene_id rx_dt;
run;/*sort by regular order*/

**************sustained inc int for next visit and cessation***************;

****sort by desc to pull the next obs using lag function***;
proc sort data= etan_6 out=sus1;  by bene_id descending rx_dt ;run;

data sus1;
set sus1;
  next1_dose_red = lag( dose_ch);
  next2_dose_red = lag (next1_dose_red);
  if event_dosered=1;
  /*keeping only obs at time of event; lag and lag of lag by descending to pull next1 and 2 obs*/

keep bene_id rx_dt dose_ch next1_dose_red next2_dose_red;
run;



/*sort by regular order*/
proc sort data= sus1; by bene_id rx_dt;run;
proc sort data= etan_6; by bene_id rx_dt;run;

data etan_7;
merge etan_6 sus1;
by bene_id rx_dt;

/*sustained in next 1 rx dt*/
if event_dosered=1 and next1_dose_red gt 0 then event_dosered_sus1=0;
else if event_dosered=1 and next1_dose_red  le 0 then event_dosered_sus1=1;

/*sustained in next 2 rx dt*/
if event_dosered =1 and event_dosered_sus1=1 and  next2_dose_red gt 0 then event_dosered_sus2=0;
else if event_dosered=1 and event_dosered_sus1=1 and next2_dose_red le 0 then event_dosered_sus2=1;


if event_dosered_sus1 =1 then sus1_dosered_dt= dose_red_dt;

format  sus1_dosered_dt date9. ;

if event_dosered_sus1 =1 and event_incint_sus1=1 then event_both=1;/*n=7obs have both incint and dosered*/
if event_both=1 then event_both_dt= incint_dt; /*both incint and dosered dt are same*/

index2= intnx ('DAY',index_dt,183);/*6mths for part D index med*/

format event_both_dt date9. index2 date9.;

run;


proc sort data= etan_7; by bene_id;run;

******************************************************************************************
***************************************CESSATION*******************************************
**********************************************************************************************;

data etan_8;
  set etan_7;

 by bene_id;

 if index_lagrx_int ge 183 and supply gt 90 then event_cess=1;

 **************get potential cess dt**************;


if event_cess=1 and idswitch ne 1 then do;
    cess_dt = lag_rx_dt;
    cess_index=1;/*ind for cess during index***/
	end;
*****************switchers, but cessation happened during primn rx**********;
  if event_cess=1 and idswitch=1 and rx_dt =<switch_dt then do;
    cess_dt= lag_rx_dt;
    cess_index=1;/*ind for cess during index***/  

    end;
***************switchers and cessation happened during the first switch******;
if event_cess=1 and idswitch=1 and (rx_dt >switch_dt) and
      switch_dt = lag_rx_dt then do;
    cess_dt= switch_dt;/* at the time of first switch*/
    cess_switch=1;

	end;

*subsequent switches switch_dt < lag_rx_dt doesnt count for cessation
 (switch_dt indicates time of FIRST swtich ,NTV)*;
   if event_cess=1 and idswitch=1 and (switch_dt < lag_rx_dt) then  event_cess= . ;

 /*2********true stop gap >90 between last rx and end of fu/death among those who were on index med*********/

  if last.bene_id and idswitch = . then do;

  if (lastrx_end - DAYS_SUPLY_NUM) gt 90 then event_cess=1;
   if (lastrx_end - DAYS_SUPLY_NUM) gt 90 and event_cess=1 then cess_dt= last_rx_dt;

 end;

*******create index2 date, dt of start of FU**********;
   index2 = intnx ('DAY',index_dt, 183);/*7mths for part D index med*/;

  format cess_dt date9.  index2 date9. ;

run;

**************get first  event for each id***********************
******************first event cannot happen before INDEX2********************
*********************4 possible desc: inc int, dec dose, both, cess*****;

**1.int**;
data int;
set etan_8;
if event_incint_sus1=1 and (sus1_incint_dt >index2);/*keep incint dates after index2*/

keep bene_id    sus1_incint_dt event_incint_sus1;

rename sus1_incint_dt= incintdt_first
event_incint_sus1= event_incint_first;
run;

proc sort data= int; by bene_id ;run;

data int1;
set int;
by bene_id;
if first.bene_id;
run;/*n=33*/

************dose reduction*****;

**2. dose**;
data dose;
set etan_7;
if event_dosered_sus1=1 and (sus1_dosered_dt >index2);;
keep bene_id   sus1_dosered_dt event_dosered_sus1;
rename sus1_dosered_dt= dosered_dt_first 
        event_dosered_sus1=event_dosered_first;
run;

proc sort data= dose; by bene_id ;run;

data dose1;
set dose;
by bene_id;
if first.bene_id;
run;/*n=14*/

**************no repeat ids with cess and both , hence first ids for not necc ************;

**3.both**;
data both;
set etan_8;
if event_both=1  and  (event_both_dt >index2);
keep bene_id   event_both event_both_dt  ;/*all n=3 obs belong to diff ids, no repeat ids**/
rename event_both_dt= eventboth_dt_first event_both= event_both_first ;/*keep event_both and rename to make ot ntv*/
run;

*****4.cessation******;

data cess;
set etan_8;
if event_cess=1 and (cess_dt >index2);
keep bene_id  event_cess  cess_dt;
rename cess_dt= cess_dt_first event_cess=event_cess_first;
run;/*140*/

proc sort data= cess; by bene_id;run;

data cess1;
set cess;
by bene_id;
if first.bene_id;
run;/*99*/

proc sort data= dose1; by bene_id ;run;
proc sort data= cess1 ; by bene_id ;run;
proc sort data= both ; by bene_id ;run;
proc sort data= int1; by bene_id ;run;
proc sort data= etan_8; by bene_id ;run;

data ra.etan_9a;
merge etan_8 cess1 both int1 dose1 ;
length desc $40;
by bene_id ;

/***if both, then code inc int and dose red as missing***/
if eventboth_dt_first ne .  and (eventboth_dt_first  = incintdt_first) and (eventboth_dt_first = dosered_dt_first)
then do;
  incintdt_first = .;
  dosered_dt_first= . ;
end;

****************************ids with only one desc event**************************;

******************ONLY INT INC******************************;
if incintdt_first ne . and  cess_dt_first = . and dosered_dt_first = . and eventboth_dt_first = . then do;
desc_dt= incintdt_first;
desc= "Interval increase";
end;

*************only CESS****************************;
else if  incintdt_first= . and  dosered_dt_first = . and cess_dt_first ne . and eventboth_dt_first = . then do;
desc_dt= cess_dt_first;
desc= "Cessation";
end;

****************only dose resuction*******************;

else if  incintdt_first= . and  dosered_dt_first ne . and cess_dt_first = .  and eventboth_dt_first = . then do;
desc_dt= dosered_dt_first;;
desc= "Dose Reduction";
end;

*************only both************************;
else if  incintdt_first = . and  dosered_dt_first = . and cess_dt_first= .   and eventboth_dt_first ne . then do;
desc_dt= eventboth_dt_first;
desc= "Dose Reduction and Interval increase";
end;

******************ids with more than one desc event*****************************************;
******cess and inc int**********************;
 else if  incintdt_first ne . and  cess_dt_first ne . and  dosered_dt_first = . and eventboth_dt_first = .  then do;

if  incintdt_first < cess_dt_first  then  desc_dt= incintdt_first;
if incintdt_first >= cess_dt_first then desc_dt= cess_dt_first;

if  incintdt_first < cess_dt_first  then desc= "Interval increase";
if incintdt_first >= cess_dt_first then desc = "Cessation";

end;

**********both cess and both****************;
else if  incintdt_first = . and  cess_dt_first ne . and  dosered_dt_first = . and eventboth_dt_first ne .  then do;

if  eventboth_dt_first < cess_dt_first  then  desc_dt= eventboth_dt_first;
if eventboth_dt_first >= cess_dt_first then desc_dt= cess_dt_first;

if  eventboth_dt_first < cess_dt_first then desc= "Dose Reduction and Interval increase";
if eventboth_dt_first >= cess_dt_first then desc = "Cessation";

end;

********both cess and dose red*******************;
else if  incintdt_first = . and  cess_dt_first ne . and  dosered_dt_first ne . and eventboth_dt_first = .  then do;


if dosered_dt_first >= cess_dt_first then desc_dt= cess_dt_first;
if  dosered_dt_first < cess_dt_first  then  desc_dt= dosered_dt_first;

if dosered_dt_first >= cess_dt_first then desc = "Cessation";
if  dosered_dt_first < cess_dt_first  then desc= "Dose Reduction";

end;

********inc nt and dose red occuring at seperate occasions****;

else if  incintdt_first ne . and  cess_dt_first = . and  dosered_dt_first ne . and eventboth_dt_first = .  then do;

if  dosered_dt_first < incintdt_first  then  desc_dt= dosered_dt_first;
if dosered_dt_first > incintdt_first then desc_dt= incintdt_first;

if  dosered_dt_first < incintdt_first  then desc= "Dose Reduction";
if dosered_dt_first > incintdt_first then desc = "Interval increase";

end;

**************both and dose red occuring seperately***************;
else if  incintdt_first = . and  cess_dt_first = . and  dosered_dt_first ne . and eventboth_dt_first ne .  then do;

if  dosered_dt_first < eventboth_dt_first  then  desc_dt= dosered_dt_first;
if dosered_dt_first > eventboth_dt_first then desc_dt= eventboth_dt_first;

if  dosered_dt_first < eventboth_dt_first  then desc= "Dose Reduction";
if dosered_dt_first > eventboth_dt_first then desc = "Dose Reduction and Interval increase";

end;

*********both inc int and both occuring seperately***************;
else if  incintdt_first ne . and cess_dt_first = . and  dosered_dt_first = . and eventboth_dt_first ne .  then do;

if   incintdt_first < eventboth_dt_first  then  desc_dt=  incintdt_first;
if  incintdt_first > eventboth_dt_first then desc_dt= eventboth_dt_first;

if   incintdt_first < eventboth_dt_first  then desc= "Interval increase";
if  incintdt_first > eventboth_dt_first then desc = "Dose Reduction and Interval increase";

end;

**********all three (no ids with both and 2 more desc events)***************************;
else if  incintdt_first ne . and cess_dt_first ne . and  dosered_dt_first ne . and eventboth_dt_first = .  then do;

if   incintdt_first < cess_dt_first < dosered_dt_first  then  desc_dt=  incintdt_first;
if  incintdt_first >  cess_dt_first > dosered_dt_first then desc_dt= dosered_dt_first;

if  incintdt_first < dosered_dt_first <  cess_dt_first then  desc_dt=  incintdt_first;
if  incintdt_first > dosered_dt_first >  cess_dt_first then  desc_dt= cess_dt_first;

if  dosered_dt_first > incintdt_first >  cess_dt_first  then desc_dt= cess_dt_first;
if  dosered_dt_first < incintdt_first <  cess_dt_first  then desc_dt= dosered_dt_first;

if  dosered_dt_first > cess_dt_first > incintdt_first    then desc_dt= incintdt_first;
if  dosered_dt_first < cess_dt_first < incintdt_first  then desc_dt= dosered_dt_first;

 if cess_dt_first =< incintdt_first  < dosered_dt_first  then  desc_dt=  cess_dt_first;
if  cess_dt_first > incintdt_first  > dosered_dt_first then desc_dt = dosered_dt_first;


 if cess_dt_first =< dosered_dt_first  < incintdt_first  then  desc_dt =  cess_dt_first;
if  cess_dt_first > dosered_dt_first  > incintdt_first then desc_dt = incintdt_first ;

***desc**;

if   incintdt_first < cess_dt_first < dosered_dt_first  then  desc= "Interval increase";
if  incintdt_first >  cess_dt_first> dosered_dt_first then desc= "Dose Reduction";

if  incintdt_first < dosered_dt_first <  cess_dt_first then  desc = "Interval increase";
if  incintdt_first > dosered_dt_first >  cess_dt_first then  desc= "Cessation";

if  dosered_dt_first > incintdt_first >  cess_dt_first  then desc = "Cessation";
if  dosered_dt_first< incintdt_first <  cess_dt_first  then desc = "Dose Reduction";

if  dosered_dt_first > cess_dt_first > incintdt_first    then desc= "Interval increase";
if  dosered_dt_first < cess_dt_first < incintdt_first  then desc = "Dose Reduction";

 if cess_dt_first =< incintdt_first  < dosered_dt_first  then  desc=  "Cessation";
if  cess_dt_first > incintdt_first  > dosered_dt_first then desc = "Dose Reduction";

 if cess_dt_first =< dosered_dt_first  < incintdt_first  then  desc =  "Cessation";
if  cess_dt_first > dosered_dt_first  > incintdt_first then desc = "Interval increase" ;

end;


*****none****;
if  incintdt_first = . and  cess_dt_first = .  and dosered_dt_first = . and  eventboth_dt_first = . 
then desc= "None";

format desc_dt date9.;

drop  next1_diff_int next2_diff_int   incint_dt_6m rx_incint6 int_6  lag_int6 diff_dose_int2
sum_rx_int sum_diff_int rx_dosered6 dose_6 lag_rx_dosered6 lag_dose6 dose_ch1 dose_ch2
dose_red_dt_6m sum_rx_int1 sum_dose_ch
next1_dose_red next2_dose_red lag_dose_d;

run; 

*******************OTHER PART D MEDS (NO DOSE REDUCTION POSSIBLE)*********************;

%macro partd_med (med= , name= );

/*dataset with index med x use*/
            
/*create possible event (inc_int and cess) and event dates*/

data &name.;
set ra.coh_ana5a;

   where index_med1= "&med" ;/*index_med1 codes for source of goli*/

   drop yr_partd yr_b  SRVC_DT  PDE_ID Medication_name diff_d 
   med_name diff_b diff_d  RA_b_icd;

 /*FLAG OBSRVATIONS with ANY increase interval (6 months after index for partD) */

   if index_lagrx_int ge 183 and diff_dose_int gt 0 then inc_dose_int=1;


/*if increase dose int occurs during swtiching meds is cessation, not interval increase*/
   if index_lagrx_int ge 183 and diff_dose_int gt 0 and (name_med ne lag_name_med) then inc_dose_int=.;


   if inc_dose_int=1 then incint_dt= lag_rx_dt;/*use the date of last time when interval is stable*/
	
/*  get event date minus 6 months to asses stability*/
   incint_dt_6m = intnx('DAY',incint_dt,-183);
  
   
/*no event in first 6 months after index30*/

 if index_rx_int lt 183 then do;

    incint_dt = . ; 
    incint_dt_6m = .;
	inc_rx_int= . ;
 
  end;

format incint_dt date9. incint_dt_6m date9.   ;

run;

**********INCREASED INTERVAL AND  STABILITY OF USE BEFROE************************
****************************************************************************;

/* SORT by descending
PURPOSE: CARRY BACKWARD to fill the missing values of date***/
proc sort data= &name out= &name._1 ;
  by bene_id descending rx_dt descending incint_dt ; run;

/*create time interval for 6 m prior to event date*/
data &name._1;
set &name._1;
 by bene_id;
 retain incint_dt_6m2 incint_dt2 ;

 if first.bene_id then do;/*setting to . at the begining for each id*/

 incint_dt_6m2= .;
 incint_dt2=. ;
 
 end;

 if incint_dt_6m > . then incint_dt_6m2= incint_dt_6m ;/*if missing then carry forward(backward)*/
 else incint_dt_6m = incint_dt_6m2;/*else keep the prev value*/


 if incint_dt > . then incint_dt2= incint_dt ;/*if missing then carry forward(backward)*/
 else incint_dt = incint_dt2;/*else keep the prev value*/

drop   incint_dt2 incint_dt_6m2 ;

run;

/**sort back by  rx_dt to regular chron order*/
proc sort data= &name._1 out= &name._2; by bene_id rx_dt ;run;

data &name._2;
  set &name._2;

   by bene_id;

rx_incint6 = rx_dt - incint_dt_6m;/* int between rx date and event dt-6m date*/

/* new var to code stable int,  set to missing at the time of event, and obs beyond 6 month dt
so stable int can be assessed from obs before event until 6 mothns before event*/

if rx_incint6 ge 0 and rx_incint6 le 183 then int_6=1; else int_6=0;
 
if rx_incint6 lt 0  or rx_incint6 gt 183 then diff_dose_int1 = . ;/*183 is the time of event*/
else diff_dose_int1= diff_dose_int;/*outside 6m window, set to . else take value of diff_int_w */


/*lag vars*/
lag_rx_incint6= lag (rx_incint6);
 lag_int6= lag (int_6);
 lag_rx_int= lag(rx_int);

if first.bene_id then do;
  lag_rx_incint6= .;
  lag_int6= .;
  lag_rx_int= .;

  end;
 
 ****code first diff in the interval between inc dt and inc dt -6m as missing, as
 this value lies outside of the time interval of interest*;

 if int_6 ne lag_int6 then diff_dose_int1= . ; 

 /*use absolute value to calculate sum of diff so that -1 and 1 diff doesnt sum upto zero*/

 diff_dose_int2 = abs (diff_dose_int1); 
  
run;

*******************interval increase******************;
******************stable use prior*********************;

proc sort data= &name._2 out= &name._int  ; by bene_id  incint_dt; run;

/*get sum of diff int for each potential event date
  value >0 indicates unstable use */
proc means data= &name._int sum noprint;
     by bene_id incint_dt;
    var diff_dose_int2;
    output out=sum_diff 
        sum=Sum_diff_int ;
run;

/*get sum of numbers of days between each potential event date 
 ( for first event it is number of days since index date
event flag are coded such that the event cannot happen in 6 months since index30 date)*/
proc means data=&name._int sum noprint;
    by bene_id incint_dt;
    var lag_rx_int;
    output out=sum_rx_int 
        sum=Sum_rx_int ;
run;


proc sort data= sum_rx_int out= sum_1 (drop= _TYPE_ _FREQ_) ; by bene_id incint_dt;run;
proc sort data= sum_diff out= sum_2 (drop= _TYPE_ _FREQ_) ; by bene_id incint_dt;run;
proc sort data= &name._2; by bene_id incint_dt ; run;


data &name._3;
merge &name._2 sum_1 sum_2;
by bene_id incint_dt;

lag_days_suply= lag (DAYS_SUPLY_NUM);

if first.bene_id then lag_days_suply= .;

  SUPPLY=  rx_int - lag_days_suply;

****************NON SWITCHERS*************;

if idswitch ne 1 then do;

if inc_dose_int=1 and sum_diff_int=0 and sum_rx_int ge 183 then event_incint=1;/* increase int AND stable diff in past 6 m*/
else event_incint=0;

end;

/*******SWITCHERS, look for event during index med only**********/
if idswitch =1 then do;

if  inc_dose_int=1 and sum_diff_int=0 and sum_rx_int ge 183 and incint_dt < switch_dt 
then event_incint=1;/*if increase int happened before switch*/

else event_incint=0;/*not stable/ not during index med*/

end;

***********IF INCREASE INTERVAL QUALIFIES AS CESSATION**************;
/*gap >90, then it qualifies for cessation and not inc interval*/

  if  SUPPLY gt 90 then event_incint = . ;

run;

proc sort data= &name._3 ; by bene_id rx_dt;
run;/*sort by regular order*/

**************sustained inc int for next visit and cessation***************;

****sort by desc to pull the next obs using lag function***;
proc sort data= &name._3 out=sus;  by bene_id descending rx_dt ;run;

data sus;
set sus;
  next1_diff_dose_int= lag( diff_dose_int);
  next2_diff_dose_int= lag (next1_diff_dose_int);

  if event_incint=1;
  /*keeping only obs at time of event; lag and lag of lag by descending to pull next1 and 2 obs*/

keep bene_id rx_dt diff_int_w next1_diff_dose_int next2_diff_dose_int;
run;

/*sort by regular order*/
proc sort data= sus; by bene_id rx_dt;run;
proc sort data= &name._3; by bene_id rx_dt;run;

data &name._3;
merge &name._3 sus;
by bene_id rx_dt;

/*sustained in next 1 rx dt*/
if event_incint=1 and next1_diff_dose_int lt 0 then event_incint_sus1=0;
else if event_incint=1 and next1_diff_dose_int ge 0 then event_incint_sus1=1;

/*sustained in next 2 rx dt*/
if event_incint=1 and event_incint_sus1=1 and  next2_diff_dose_int lt 0 then event_incint_sus2=0;
else if event_incint=1 and event_incint_sus1=1 and next1_diff_dose_int ge 0 then event_incint_sus2=1;

   if event_incint_sus1 =1 then sus1_incint_dt= incint_dt;

      format sus1_incint_dt date9.;

run;

proc sort data= &name._3 ; by bene_id;run;

***************CESSATION***********;

data  &name._3a;
    set &name._3;
    by bene_id;

if index_lagrx_int ge 183 and supply gt 90 then event_cess=1; 

 **************get potential cess dt**************;


if event_cess=1 and idswitch ne 1 then do;
    cess_dt = lag_rx_dt;
    cess_index=1;/*ind for cess during index***/
	end;

*****************switchers, but cessation happened during primn rx**********;
  if event_cess=1 and idswitch=1 and rx_dt =<switch_dt then do;
    cess_dt= lag_rx_dt;
    cess_index=1;/*ind for cess during index***/  

    end;

***************switchers and cessation happened during the first switch******;
if event_cess=1 and idswitch=1 and (rx_dt >switch_dt) and
      switch_dt = lag_rx_dt then do;
    cess_dt= switch_dt;/* at the time of first switch*/
    cess_switch=1;

	end;

*subsequent switches switch_dt < lag_rx_dt doesnt count for cessation
 (switch_dt indicates time of FIRST swtich ,NTV)*;
   if event_cess=1 and idswitch=1 and (switch_dt < lag_rx_dt) then  event_cess= . ;

/*2********true stop gap >90 between last rx and end of fu/death among those who were on index med*********/

  if last.bene_id and idswitch = . then do;

  if (lastrx_end - DAYS_SUPLY_NUM) gt 90 then event_cess=1;
   if (lastrx_end - DAYS_SUPLY_NUM) gt 90 and event_cess=1 then cess_dt= last_rx_dt;

 end;
*******create index2 date, dt of start of FU**********;
   index2 = intnx ('DAY',index_dt, 183);/*6mths for part d index med*/;

  format cess_dt date9.  index2 date9. ;

run;

**************get first increase int event for each id***********************;
**int**;
data int;
set &name._3a;
if event_incint_sus1=1 and (sus1_incint_dt> index2);
keep bene_id   sus1_incint_dt;
rename sus1_incint_dt= incintdt_first;
run;

proc sort data= int; by bene_id ;run;

data int1;
set int;
by bene_id;
if first.bene_id;
run;

proc sort data= int1; by bene_id ;run;
proc sort data= &name._3a; by bene_id ;run;

data &name._4;
merge &name._3a int1 ;
by bene_id ;
run;


****************** get ids with cessation ***************;

data cess;
set &name._4;
if event_cess=1 and (cess_dt> index2);
keep bene_id   cess_dt;
rename cess_dt= cess_dt_first;
run;


proc sort data= cess; by bene_id ;run;


data cess1;
set cess;
by bene_id;
if first.bene_id;
run;
proc sort data= &name._4; by bene_id ;run;

data &name._5;
merge &name._4 cess1 ;
by bene_id ;
run;

**************get first desc date******************;
data ra.&name._5a;
set &name._5;

**************only inc int****************************;
if incintdt_first ne . and  cess_dt_first = . then do;
desc_dt= incintdt_first;
desc= "Interval increase";
end;

*************only cess********************************;
else if  incintdt_first= . and  cess_dt_first ne . then do;
desc_dt= cess_dt_first;
desc= "Cessation";
end;

***************both**********************;

 else if  incintdt_first ne . and  cess_dt_first ne . then do;

if  incintdt_first < cess_dt_first  then desc_dt= incintdt_first;
if incintdt_first > cess_dt_first then desc_dt= cess_dt_first;

if  incintdt_first < cess_dt_first  then desc= "Interval increase";
if incintdt_first >= cess_dt_first then desc = "Cessation";

end;

if  incintdt_first = . and  cess_dt_first = . then desc= "None";

format desc_dt date9.;

run;

%mend;

%partd_med (med= Adalimumab , name= ada );
%partd_med (med= Certolizumab , name= cert );
%partd_med (med= Goli_d , name= goli_d );









